Routes
Basic
Connect URLs to Code
- When your Rails application receives an incoming request for
GET /patients/17
- It asks the router to match it to a controller action. If the first matching route is
get '/patients/:id', to: 'patients#show'
- The request is dispatched to the
patients
controller'sshow
action with{id: '17'}
inparams
- Rails uses snake_case for controller names here, if you have a multiple word controller like
MonsterTruckController
, you want to usemonster_truck#show
for example
Configuring the Rails Router
- The router for your application or engine live in the file
config/routes.rb
and typically looks like this
Routes Controller Mapping
- Rails will map the route to its corresponding controller according to this syntax
resource :<resource_name> => <resource_name>Controller
- E.g:
resources :brands => BrandsController, :basket => BasketController
- NOTES: the mapping only work if the controllers are directly under
app/controllers
directory, for further customization, see [[Routes#Namespace | Namespace]]
Resource Routing: the Rails default
- Resource routing allows you to quickly declare all of the common routes for a given resourceful controller.
- A single call to
resource
can declare all the necessary routes- index
- show
- new
- edit
- create
- update
- destroy
- A resourceful route provides a mapping between HTTP verbs and URLs to controller actions.
- NOTES: If. using
--api
when creating application, the routeindex
andnew
will not be created (as we are dealing with [[Rails for API only (what I need) | API-only application]])
Singular Resource
- For example, you would like
/profile
to always show the profile of the currently logged in user. In this case you can use a singular resource to map/profile
to theshow
actionget 'profile', to: 'users#show'
- Passing a
String
toto:
will expect acontroller#action
format. When using aSymbol
, theto:
option should be replaced withaction:
. When using aString
without a#
, theto:
option should be replaced withcontroller:
get 'profile', action: :show, controller: 'users'
Namespace
- You may wish to organize groups of controllers under a namespace.
- For example: you might group a number of administrative controllers under and
Admin::
namespace, and place these controllers under theapp/controllers/admin
directory. You can route to such a group by using anamespace block
- This will create a number of routes for each of the
articles
andcomments
controller. ForAdmin::ArticlesController
, Rails will create
Nested Resources
- It's common to have resources that are logically children of other resources.
- E.g
- Nested routes allow you to capture this relationship in your routing. In this case, you could include this in route declartion
Adding More RESTful Actions
- We are not limited to the 7 routes that RESTful routing creates by default
- To add a member route, just add a
member
block into the resource block - This will recognize
/photos/1/preview
with GET, and route to thepreview
action ofPhotosController
, with the resource id value passed inparams[:id]